<template>
  <div class="app-container">
    <el-button
      class="filter-item"
      style="margin-left: 10px"
      type="primary"
      icon="el-icon-edit"
      @click="handleCreate"
    >
      添加营业单位
    </el-button>
    <br />

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="{row}">
          {{ row.id }}
        </template>
      </el-table-column>

      <el-table-column label="名称">
        <template slot-scope="{ row }">
          <template v-if="row.edit">
            <el-input v-model="row.name" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="上级营业单位编号" width="150">
        <template slot-scope="{ row }">
          <template v-if="row.edit">
            <el-input v-model="row.parentId" class="edit-input" size="small" />
          </template>
          <span v-else>{{ row.parentId }}</span>
        </template>
      </el-table-column>

      <el-table-column  label="终端编号" width="150">
        <template slot-scope="{row}">
          {{row.terminalId}}
        </template>
      </el-table-column>

      <el-table-column
        class-name="status-col"
        label="类型"
        width="110"
        align="center"
      >
        <template slot-scope="scope">
<<<<<<< HEAD
          <el-tag :type="scope.row.status | statusFilter">{{
            scope.row.status
          }}</el-tag>
=======
          <el-tag :type="scope.row.type | statusFilter">{{ scope.row.type }}</el-tag>
>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
        </template>
      </el-table-column>
      <!--      <el-table-column align="center" prop="created_at" label="Display_time" width="200">-->
      <!--        <template slot-scope="scope">-->
      <!--          <i class="el-icon-time" />-->
      <!--          <span>{{ scope.row.display_time }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
<<<<<<< HEAD
=======

>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f

      <el-table-column label="其他">
        <template slot-scope="{ row }">
          <template v-if="row.edit">
<<<<<<< HEAD
            <el-input v-model="row.title" class="edit-input" size="small" />
=======
            <el-input v-model="row.description" class="edit-input" size="small" />

>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
          </template>
          <span v-else>{{ row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="220">
<<<<<<< HEAD
        <template slot-scope="{ row }">
=======
        <template slot-scope="{row,$index}">
>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
          <el-button
            v-if="row.edit"
            type="success"
            size="small"
            icon="el-icon-circle-check-outline"
            @click="confirmEdit(row)"
          >
            Ok
          </el-button>
          <el-button
            v-if="row.edit"
            size="small"
            icon="el-icon-refresh"
            type="warning"
            @click="cancelEdit(row)"
          >
            取消
          </el-button>
          <el-button
            v-else
            type="primary"
            size="small"
            icon="el-icon-edit"
            @click="row.edit = !row.edit"
          >
            编辑
          </el-button>
<<<<<<< HEAD
          <el-button
            v-if="!row.edit"
            @click="delete scope.row"
            type="danger"
            size="small"
            >删除</el-button
          >
=======
          <el-button v-if="!row.edit"@click="handleDelete(row,$index)" type="danger" size="small">删除</el-button>
>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加营业单位" :visible.sync="dialogFormVisible">
<<<<<<< HEAD
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="140px"
        style="width: 400px; margin-left: 50px"
      >
=======
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="140px" style="width: 400px; margin-left:50px;">
>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
        <!--        <el-form-item label="Type" prop="type">-->
        <!--          <el-select v-model="temp.type" class="filter-item" placeholder="Please select">-->
        <!--            <el-option v-for="item in calendarTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="Date" prop="timestamp">-->
        <!--          <el-date-picker v-model="temp.timestamp" type="datetime" placeholder="Please pick a date" />-->
        <!--        </el-form-item>-->
        <el-form-item label="名称" prop="title">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="上级营业单位编号">
          <el-input v-model="temp.parentId" />
        </el-form-item>
        <!--        <el-form-item label="Imp">-->
        <!--          <el-rate v-model="temp.importance" :colors="['#99A9BF', '#F7BA2A', '#FF9900']" :max="3" style="margin-top:8px;" />-->
        <!--        </el-form-item>-->
        <el-form-item label="描述">
          <el-input
            v-model="temp.description"
            :autosize="{ minRows: 2, maxRows: 4 }"
            type="textarea"
            placeholder="Please input"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消 </el-button>
        <el-button type="primary" @click="createData()"> 确定 </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
<<<<<<< HEAD
import { getList, createArticle } from "@/api/table";

const calendarTypeOptions = [
  { key: "CN", display_name: "China" },
  { key: "US", display_name: "USA" },
  { key: "JP", display_name: "Japan" },
  { key: "EU", display_name: "Eurozone" },
];

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      list: null,
      listLoading: true,
      temp: {
        id: undefined,
        description: "",
        parentId: "",
      },
      calendarTypeOptions,
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "Edit",
        create: "Create",
      },
      dialogPvVisible: false,
      listQuery: {
        page: 1,
        limit: 10,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // fetchData() {
    //   this.listLoading = true
    //   getList().then(response => {
    //     this.list = response.data.items
    //     this.listLoading = false
    //   })
    // },
    async getList() {
      this.listLoading = true;
      const { data } = await getList(this.listQuery);
      console.log(data);

      const items = data.items;
      this.list = items.map((v) => {
        this.$set(v, "edit", false); // https://vuejs.org/v2/guide/reactivity.html
        v.originalTitle = v.title; //  will be used when user click the cancel botton
        v.originalPageView = v.pageviews;
        return v;
      });
      this.listLoading = false;
    },
    update() {},
    delete(row, index) {
      this.$notify({
        title: "Success",
        message: "Delete Successfully",
        type: "success",
        duration: 2000,
      });
      this.list.splice(index, 1);
    },
    onSubmit() {
      this.$message("submit!");
    },
    onCancel() {
      this.$message({
        message: "cancel!",
        type: "warning",
      });
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.temp.id = parseInt(Math.random() * 100) + 1024; // mock a id
          this.temp.author = "vue-element-admin";
          createArticle(this.temp).then(() => {
            this.list.unshift(this.temp);
            this.dialogFormVisible = false;
            this.$notify({
              title: "Success",
              message: "Created Successfully",
              type: "success",
              duration: 2000,
            });
          });
        }
      });
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: "",
        timestamp: new Date(),
        title: "",
        status: "published",
        type: "",
      };
    },
    cancelEdit(row) {
      row.title = row.originalTitle;
      row.pageviews = row.originalPageView;
      row.edit = false;
      this.$message({
        message: "编辑已取消",
        type: "warning",
      });
    },
    confirmEdit(row) {
      row.edit = false;
      row.originalTitle = row.title;
      row.originalPageView = row.pageviews;
      this.$message({
        message: "编辑已提交",
        type: "success",
      });
    },
  },
};
=======
  import { getList ,createEntity,deleteEntity} from '@/api/consume'

  const calendarTypeOptions = [
    { key: 'CN', display_name: 'China' },
    { key: 'US', display_name: 'USA' },
    { key: 'JP', display_name: 'Japan' },
    { key: 'EU', display_name: 'Eurozone' }
  ]

  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        list: null,
        listLoading: true,
        temp: {
          id: undefined,
          description: '',
          parentId: '',
        },
        calendarTypeOptions,
        dialogFormVisible: false,
        dialogStatus: '',
        textMap: {
          update: 'Edit',
          create: 'Create'
        },
        dialogPvVisible: false,
        listQuery: {
          page: 1,
          limit: 10
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {

      // fetchData() {
      //   this.listLoading = true
      //   getList().then(response => {
      //     this.list = response.data.items
      //     this.listLoading = false
      //   })
      // },
      async getList() {
        this.listLoading = true
        const { data } = await getList(this.listQuery)
        const items = data.items
        const length = data.length
        this.list = items.map(v => {
          this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
          v.originalName = v.name//  will be used when user click the cancel botton
          v.originalParentId = v.parentId
          v.originalDescription = v.description
          return v
        })
        this.listLoading = false
      },
      update(){

      },
      handleDelete(row,index) {
        //this.list.splice(index,1)
        //console.log('12344556678')
        this.list.splice(index, 1)
        deleteEntity(row).then(() => {
          this.$notify({
            title: 'Success',
            message: 'Created Successfully',
            type: 'success',
            duration: 2000
          })
        })


      },
      onSubmit() {
        this.$message('submit!')
      },
      onCancel() {
        this.$message({
          message: 'cancel!',
          type: 'warning'
        })
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            //this.temp.id = parseInt(Math.random() * 100) + 1024 // mock a id
            //this.temp.author = 'vue-element-admin'
            createEntity(this.temp).then(() => {
              this.temp.id = length + 1;
              this.temp.type = 'draft'
              this.temp.terminalId = '12344'
              this.list.push(this.temp)
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
            })
          }
        })
      },
      resetTemp() {
        this.temp = {
          id: undefined,
          parentId: '',
          description: ''
        }
      },
      cancelEdit(row) {
        row.name = row.originalName
        row.parentId = row.originalParentId
        row.description = row.originalDescription
        row.edit = false
        this.$message({
          message: '编辑已取消',
          type: 'warning'
        })
      },
      confirmEdit(row) {
        row.edit = false
        row.originalName = row.name
        row.originalParentId = row.parentId
        row.originalDescription = row.description
        this.$message({
          message: '编辑已提交',
          type: 'success'
        })
      },
    }
  }
>>>>>>> 9bd91f7cdea89e78ee433b17f8909db13645484f
</script>
